/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       timed_event.cpp

	$Header: /game/timed_event.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "timed_event.h"

#include "sized_int_types.h"
#include "streambuf_operators.h"

// --------------------------------------------------------------------------
// Unnamed namespace
// --------------------------------------------------------------------------

namespace
{

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	inline int to_base_map_format_version( int format_version )
	{
		return 0;
	}

} // Unnamed namespace

// --------------------------------------------------------------------------
// t_timed_event members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_timed_event::is_active_on_day( int day ) const
{
	day++;
	return (	(day == get_first_occurrence())
		||
			((day > get_first_occurrence()) 
			&&
			(get_recurrence_interval() != 0)
			&&
			( ((day - get_first_occurrence()) % get_recurrence_interval()) == 0)) );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_timed_event::read(
	std::streambuf &	stream,
	int					version )
{
	if ( !t_discrete_event::read( stream, version ) )
		return false;

	m_first_occurrence = get< t_uint16 >( stream );
	m_recurrence_interval = get< t_uint16 >( stream );

	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_timed_event::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_discrete_event::read_from_map( stream, to_base_map_format_version( format_version ) ) )
		return false;

	// Read and discard the name
	std::string name;
	if ( !read_string16( stream, name ) )
		return false;

	m_first_occurrence = get< t_uint16 >( stream );
	m_recurrence_interval = get< t_uint16 >( stream );

	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_timed_event::write( std::streambuf & stream ) const
{
	if ( !t_discrete_event::write( stream ) )
		return false;

	put< t_uint16 >( stream, m_first_occurrence );
	put< t_uint16 >( stream, m_recurrence_interval );

	return true;
}
